package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import dao.MemOriginDao;
import entity.MemOrigin;
import util.DBUtils;

public class MemOriginDaoImpl implements MemOriginDao {

	@Override
	public List<MemOrigin> selectAll() {
		try(Connection c = DBUtils.getConnection();){
			String sql = "select origin_id,origin_name,create_time,modify_time from mem_origin"
					+ " where delete_flag=0";
			PreparedStatement ps = c.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			List<MemOrigin> list = new ArrayList<>();
			while (rs.next()) {
				MemOrigin ori = new MemOrigin();
				ori.setOriginId(rs.getInt(1));
				ori.setOriginName(rs.getString(2));
				ori.setCreateTime(rs.getTimestamp(3));
				ori.setModifyTime(rs.getTimestamp(4));
				list.add(ori);
			}
			return list.isEmpty() ? Collections.emptyList() : list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return Collections.emptyList();
	}

}
